package com.linln.frontend.controller;


import com.linln.common.data.PageSort;
import com.linln.common.data.URL;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.system.domain.*;
import com.linln.modules.system.repository.*;
import com.linln.modules.system.service.ArticleService;
import com.linln.modules.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * created by Wuwenbin on 2018/7/31 at 21:33
 *
 * @author wuwenbin
 */
@Controller
public class IndexController  {

    @Autowired
    private WordsRepository wordsRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    UvRepository uvRepository;
    @Autowired
    ParamRepository paramRepository;

    @RequestMapping(value = {"", "/index"})
    public String index(Model model) {

        // 创建分页对象  查询热门的  数量为6
        PageRequest page = PageSort.pageRequest(6,"views", Sort.Direction.DESC);
        // 封装数据
        List<Article> lists=articleRepository.findAll(page).getContent();
        model.addAttribute("list", lists);

      // 创建分页对象  查询热门的  数量为6
        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();
        // 获取数据列表
        Comment comment = new Comment();
        comment.setType(1);
        Example<Comment> example = Example.of(comment, matcher);
        page = PageSort.pageRequest(5,"createDate", Sort.Direction.DESC);
        model.addAttribute("messages", commentRepository.findAll(example,page).getContent());

        model.addAttribute("countComment", commentRepository.countComment());

        page = PageSort.pageRequest(30,"createDate", Sort.Direction.DESC);
        model.addAttribute("users", userRepository.findAll(page).getContent());


        model.addAttribute("words", wordsRepository.findRandWords());
        Param param =paramRepository.findFirstByName("visitor_counts");
        int newCount= uvRepository.findCountBefore();
        model.addAttribute("visitor",Integer.valueOf(param.getValue())+newCount);
        return "/Index";
    }

}
